package com.by.course.config.web.exception;

import com.by.course.entity.base.BaseResult;
import com.by.course.execption.BusinessException;
import com.github.binarywang.wxpay.exception.WxPayException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * <p>
 * 登录模块全局异常处理
 * </p>
 *
 * @author liujingeng
 * @since 2021/04/20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 微信接口异常
     *
     * @param e WxErrorException
     * @return com.by.star.vo.base.BaseResult<java.lang.String>
     * @author zhd
     * @date 2023/08/11 16:39
     */
    @ExceptionHandler(WxPayException.class)
    public BaseResult<String> exceptionHandler(WxPayException e) {
        log.error("[WxPayException warn] message: ", e);
        return BaseResult.builder().setCode(500).setMsg(e.getMessage()).build();
    }

    /**
     * 微信接口异常
     *
     * @param e WxErrorException
     * @return com.by.star.vo.base.BaseResult<java.lang.String>
     * @author zhd
     * @date 2023/08/11 16:39
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResult<String> exceptionHandler(BusinessException e) {
        if (e.getCode() == 0) {
            e.setCode(500);
        }
        log.error("[BusinessException warn] message: ", e);
        return BaseResult.builder().setCode(e.getCode()).setMsg(e.getMsg()).build();
    }

    /**
     * 微信接口异常
     *
     * @param e WxErrorException
     * @return com.by.star.vo.base.BaseResult<java.lang.String>
     * @author zhd
     * @date 2023/08/11 16:39
     */
    @ExceptionHandler(WxErrorException.class)
    public BaseResult<String> exceptionHandler(WxErrorException e) {
        log.error("[WxErrorException warn] message: ", e);
        return BaseResult.builder().setCode(500).setMsg("微信接口异常").build();
    }

    /**
     * 参数效验异常处理器 boot @Valid 注解
     *
     * @param e 参数验证异常
     * @return BaseResult
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BaseResult<String> parameterExceptionHandler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        // 获取异常信息
        BindingResult exceptions = e.getBindingResult();
        // 判断异常中是否有错误信息，如果存在就使用异常中的消息，否则使用默认消息
        if (exceptions.hasErrors()) {
            List<ObjectError> errors = exceptions.getAllErrors();
            if (!errors.isEmpty()) {
                // 这里列出了全部错误参数，按正常逻辑，只需要第一条错误即可
                FieldError fieldError = (FieldError) errors.get(0);
                return BaseResult.builder().setCode(500).setMsg(fieldError.getDefaultMessage()).build();
            }
        }
        return BaseResult.builder().setCode(500).setMsg("参数校验异常").build();
    }

    @ExceptionHandler(Exception.class)
    public BaseResult<String> defaultHandler(Exception e) {
        log.error("[Exception catch] message: ", e);
        return BaseResult.builder().setCode(500).setMsg("服务器忙").build();
    }
}
